<?php

class Controller extends PXFController 
{
	public function __construct()
	{
		parent::__construct(); 
	}

	public function form()
	{
		PXFConfig::$site['page_title'] = 'Cont&aacute;ctenos';
		$this->tpl->assign('site',PXFConfig::$site);
		$this->tpl->display('contact.form.tpl');
	}

	public function submit()
	{

		PXFConfig::$site['page_title'] = 'Cont&aacute;ctenos';

		$mail = new ValidatorEmail('mail',true);
		$text = new ValidatorSimpleText('content',true);
		$name = new ValidatorSimpleText('name',true);

		$validator = PXFValidator::getNew();
		$validator->addItem($mail);
		$validator->addItem($text);
		$validator->addItem($name);

		$errors = $validator->validate();
		$contacts = PXFModel::getModel('contact');

		/* Create contact. */
		$contact = $contacts->getNew();
		$contact->user_id = PXFInstance::$user->id;
		$contact->name = $name->getValue();
		$contact->mail = $mail->getValue();
		$contact->content = $text->getValue();
		$contact->created_at = date('Y-m-d h:i:s');

		if(count($errors) == 0)
		{						
			$contact->save(); 
			PXFInstance::pushFlashMessage(PXFConfig::MSG_OK,'Su consulta ha sido enviada correctamente');
			$smarty = '';
			PXFRequest::redirect(build_url(array(),$smarty));
		}	
		else
		{
			PXFInstance::displayFlashMessage(PXFConfig::MSG_ERROR,'Error al enviar el formulario');
			$this->tpl->assign('errors',$errors);
			$this->tpl->assign('site',PXFConfig::$site);
			$this->tpl->display('contact.form.tpl');
		}
	}

}

?>
